import time
from distutils.log import Log

import requests
import json

ua = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Mobile Safari/537.36 Edg/93.0.961.47"
userIP = "114.80.56.117"
requestURL = "https://m.ctrip.com/restapi/soa2/21881/json/GetReviewList"

PageNo = 0
while (True):
    PageNo += 1
    requestPayload = {
        "PageNo": PageNo,
        "PageSize": 10,
        "MasterHotelId": 801226,
        "NeedFilter": True,
        "UnUsefulPageNo": 1,
        "UnUsefulPageSize": 5,
        "isHasFold": False,
        "head": {
            "Locale": "zh-CN",
            "Currency": "CNY",
            "Device": "PC",
            "UserIP": "144.0.138.159",
            "Group": "",
            "ReferenceID": "",
            "UserRegion": "CN",
            "AID": "5376",
            "SID": "167068",
            "Ticket": "",
            "UID": "",
            "IsQuickBooking": "",
            "ClientID": "1629588067447.42indi",
            "OUID": "",
            "TimeZone": "8",
            "P": "20407039943",
            "PageID": "102003",
            "Version": "",
            "HotelExtension": {
                "WebpSupport": True,
                "group": "CTRIP",
                "Qid": "659785512227",
                "hasAidInUrl": False
            },
            "Frontend": {
                "vid": "1629588067447.42indi",
                "sessionID": 5,
                "pvid": 22
            }
        },
        "ServerData": ""
    }
    data = json.dumps(requestPayload)
    try:
        html = requests.post(requestURL, data=data, headers={'User-Agent': ua})
        reviewList = json.loads(html.text)['Response']['ReviewList']
        # print(reviewList[0]['reviewDetails']['reviewContent'])
        for i in reviewList:
            print(i['reviewDetails']['reviewContent'])
    except Exception as e:
        print('爬取第' + str(PageNo) + '页评论失败，异常：' + str(e))
        time.sleep(1)
        PageNo -= 1
        break
